﻿using System;

namespace Creventive.Wpf.ShellFactory.Utility
{
    /// <summary>
    /// Base class for sealable objects.
    /// </summary>
    public abstract class Sealable : ISealable
    {
        private bool isSealed = false;

        /// <summary>
        /// Gets a value indicating whether this instance has been sealed.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance has been sealed; otherwise, <c>false</c>.
        /// </value>
        public bool IsSealed
        {
            get { return this.isSealed; }
        }

        /// <summary>
        /// Throws an exception if this instance has been sealed. Should be used at the beginning of all methods and properties which makes changes to this instance.
        /// </summary>
        protected void VerifyUnsealed()
        {
            if (this.isSealed)
                throw new InvalidOperationException("Cannot modify sealed object");
        }

        /// <summary>
        /// Seals this instance.
        /// </summary>
        public void Seal()
        {
            if (!this.isSealed)
            {
                this.OnSealing();
                this.isSealed = true;
            }
        }

        /// <summary>
        /// Called when this object is about to be sealed.
        /// </summary>
        protected virtual void OnSealing()
        {}
    }
}